-- LuaTools需要PROJECT和VERSION这两个信息
PROJECT = 'PWM fan'
VERSION = '1.0.0'

-- 引入必要的库文件(lua编写), 内部库不需要require
local sys = require 'sys'
-- 旋转编码器
-- 正反方向根据实际情况决定
local s1_pin = 18
local s2_pin = 19
local pin_need = 1
local s1_last = nil
local s2_last = nil
local s1_up = false
local s2_up = false
local s1_down = false
local s2_down = false
local pwm_ID = 5
local _,us_tick = mcu.tick64()

spi_lcd = spi.deviceSetup(5,pin.PC14,0,0,8,48*1000*1000,spi.MSB,1,1)
lcd.init("st7735s",{port = "device",pin_dc = pin.PE08, pin_pwr = pin.PE09, pin_rst = pin.PC12,direction = 3,w = 160,h = 80,xoffset = 0,yoffset = 24},spi_lcd)
lcd.invoff()
local speed = 0

us_tick = us_tick * 1000 --低于1ms的当杂波去除，可以根据实际需要修改

local function stage1()
    pin_need = 0
    -- log.info("wait down")
    ioqueue.exti(s1_pin, nil, gpio.FALLING, true)
    ioqueue.exti(s2_pin, nil, gpio.FALLING, true)
end

local function stage2()
    pin_need = 1
    -- log.info("wait up")
    ioqueue.exti(s1_pin, nil, gpio.RISING, true)
    ioqueue.exti(s2_pin, nil, gpio.RISING, true)
    s1_up,s2_up,s1_down,s2_down = false,false,false,false
end

local function s1_irq(val, tick)
    if gpio.get(s1_pin) ~= pin_need then return end
    if s1_last then
        local result,_ = mcu.dtick64(tick, s1_last, us_tick)
        if result then
            -- log.info("s1", mcu.dtick64(tick, s1_last))
            if pin_need > 0 then
                s1_up = true
            else
                s1_down = true
            end
            -- log.info("s1", s1_up, s1_down)
        else
            return
        end
    else
        -- log.info("s1")
        s1_up = true
    end
    if pin_need > 0 and s1_up and s2_up then
        stage1()
    end
    if s2_down then
        log.info("s2pin先检测到，右转+1")
        stage2()
		if speed >= 0 and speed < 100 then
		
			speed = speed + 10
		else
		    speed = 100
	    end
    end
    s1_last = tick

end

local function s2_irq(val, tick)
    if gpio.get(s2_pin) ~= pin_need then return end
    if s2_last then
        local result,_ = mcu.dtick64(tick, s2_last, us_tick)
        if result then 
            -- log.info("s2", mcu.dtick64(tick, s2_last))
            if pin_need > 0 then
                s2_up = true
            else
                s2_down = true
            end
            -- log.info("s2", s2_up, s2_down)
        else
            return
        end
    else
        -- log.info("s2")
        s2_check = true
    end
    if pin_need > 0 and s1_up and s2_up then
        stage1()
    end
    if s1_down then
        log.info("s1pin先检测到，左转+1")
        stage2()
		if speed <= 100 and speed > 0 then
		
			speed = speed - 10
		else
		    speed = 0
	    end
    end
    s2_last = tick
end

sys.subscribe("IO_QUEUE_EXTI_" .. s1_pin, s1_irq)
sys.subscribe("IO_QUEUE_EXTI_" .. s2_pin, s2_irq)

function rotary_start()
    pin_need = 1
    ioqueue.exti(s1_pin, nil, gpio.RISING, true)
    ioqueue.exti(s2_pin, nil, gpio.RISING, true)
    -- sys.timerLoopStart(clear, 10000)
end
sys.taskInit(function()
    
    
log.info("lvgl", lvgl.init()) --初始化 lvgl
if lvgl.theme_set_act then
    -- 切换主题
    -- lvgl.theme_set_act("default")
   --  lvgl.theme_set_act("mono")
   --lvgl.theme_set_act("empty")
    -- lvgl.theme_set_act("material_light")
    lvgl.theme_set_act("material_dark")
     --lvgl.theme_set_act("material_no_transition")
    -- lvgl.theme_set_act("material_no_focus")
end

--lvgl.disp_set_bg_color(nil, 0x000000) --设置 lcd 背景色



 scr = lvgl.obj_create(nil, nil) --创建屏幕
--[[ btn = lvgl.btn_create(scr)
lvgl.obj_set_size(btn,130,18)
lvgl.obj_align(btn, lvgl.scr_act(), lvgl.ALIGN_CENTER,demo_x,demo_y)
 label_demo = lvgl.label_create(btn) --创建按钮文字
lvgl.label_set_recolor(label_demo,true)
lvgl.label_set_text(label_demo, "#00ff00 Air105-MP3 #") ]]--创建按钮文字，此处根据按键显示不同的模式

label_demo =lvgl.label_create(scr)
--lvgl.obj_set_size(label_time,120,25)
lvgl.obj_set_pos(label_demo,10,20);
--lvgl.obj_set_pos(label_time, 0, 12);
--lvgl.obj_align(label_time, lvgl.scr_act(), lvgl.ALIGN_CENTER, 0, 20)
lvgl.label_set_recolor(label_demo,true)
lvgl.label_set_text(label_demo, "#00ff00 Air105 #")    

font = lvgl.font_get("opposans_m_12")


lvgl.scr_load(scr)
lvgl.obj_set_style_local_text_font(lvgl.scr_act(), lvgl.OBJ_PART_MAIN, lvgl.STATE_DEFAULT, font)



    pwm.open(5, 40000, 0)
	--pwm.open(2, 1000, 1)
	rotary_start()
    while true do
       
       
      --[[  speed = speed + 10
        if speed > 100 then speed = 0 end
        log.info("pwm","speed now",speed,"%")]]
		--log.info("pwm","speed now",speed,"%")
        pwm.open(pwm_ID, 40*1000, speed)
        lvgl.label_set_text(label_demo, "#00ff00 风速:"..speed.."% #")  
		--pwm.open(2, 1000, speed)
        sys.wait(100)
  
    end
end)

sys.run()
-- sys.run()之后后面不要加任何语句!!!!!